package com.axonactive.yolo.service.implement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.axonactive.yolo.dao.interfaces.TaskNotificationRepository;
import com.axonactive.yolo.domain.TaskNotification;
import com.axonactive.yolo.dto.TaskNotificationDTO;
import com.axonactive.yolo.exception.TaskNotificationNotFoundException;
import com.axonactive.yolo.service.internal.TaskNotificationService;

@Service
public class TaskNotificationServiceImpl implements TaskNotificationService{

	@Resource
	private TaskNotificationRepository taskNotificationRepository;
	
	@Override
	public List<TaskNotification> findByUserIdAndStatus(int userId, int status) {
		return taskNotificationRepository.findByUserIdAndStatus(userId, status);
	}

	@Override
	public List<TaskNotification> findByUserId(int userId) {
		return taskNotificationRepository.findByUserId(userId);
	}

	@Override
	public List<TaskNotification> findByTaskId(String taskId) {
		return taskNotificationRepository.findByTaskId(taskId);
	}

	@Override
	public List<TaskNotificationDTO> convertFromTaskNotification(
			List<TaskNotification> taskNotifications) {
		
		List<TaskNotificationDTO> taskNotificationDTOs = new ArrayList<>();
		TaskNotificationDTO taskNotificationDTO;
		
		for (int i = 0; i < taskNotifications.size(); i++) {
			taskNotificationDTO = new TaskNotificationDTO(taskNotifications.get(i));
			
			taskNotificationDTOs.add(taskNotificationDTO);
		}
		
		return taskNotificationDTOs;
	}

	@Override
	public TaskNotification create(TaskNotification taskNotification) {
		return taskNotificationRepository.save(taskNotification);
	}

	@Override
	public TaskNotification findByTaskIdAndUserId(String taskId, int userId) {
		List<TaskNotification> temps = taskNotificationRepository.findByTaskIdAndUserId(taskId, userId);
		
		if (temps.size() == 0) {
			return null;
		}
		else {
			return temps.get(0);
		}
	}

	@Override
	public TaskNotification findById(int id) {
		return taskNotificationRepository.findOne(id);
	}

	@Override
	public boolean update(TaskNotification taskNotification) throws TaskNotificationNotFoundException{
		TaskNotification updateTaskNotification = taskNotificationRepository.findOne(taskNotification.getId());
		
		if (updateTaskNotification == null) {
			return false;
		}
		
		updateTaskNotification.setId(taskNotification.getId());
		updateTaskNotification.setIsRead(taskNotification.getIsRead());
		updateTaskNotification.setSaveTime(taskNotification.getSaveTime());
		updateTaskNotification.setStatus(taskNotification.getStatus());
		updateTaskNotification.setTaskId(taskNotification.getTaskId());
		updateTaskNotification.setTaskInfo(taskNotification.getTaskInfo());
		updateTaskNotification.setTaskName(taskNotification.getTaskName());
		updateTaskNotification.setUserId(taskNotification.getUserId());
		
		taskNotificationRepository.save(updateTaskNotification);
		return true;
	}

	@Override
	public boolean delete(int id) throws TaskNotificationNotFoundException {
		TaskNotification taskNotification = taskNotificationRepository.findOne(id);
		
		if (taskNotification != null) {
			taskNotificationRepository.delete(id);
			
			return true;
		}
		
		return false;
	}

}
